package com.botann.projects.controller;


import com.botann.projects.base.Model;
import com.botann.projects.constants.API;
import com.botann.projects.constants.Constant;
import com.botann.projects.base.BaseController;
import com.botann.projects.base.Model;
import com.botann.projects.constants.API;
import com.botann.projects.constants.Constant;
import com.botann.projects.model.Menu;
import com.botann.projects.model.View;
import com.botann.projects.model.ViewItem;
import com.botann.projects.service.MenuServiceIntf;
import com.botann.projects.service.ViewServiceIntf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * Charging
 * ViewController
 * Created by 刘山国 on 2018/4/16.
 * Copyright (c) 2017 伯坦科技. All rights reserved
 */
@RestController
public class ViewController extends BaseController {

    @Override
    public Class loggerClass() {
        return ViewController.class;
    }
    @Autowired
    private ViewServiceIntf service;

    @Autowired
    private MenuServiceIntf menuService;


    /**
     * 页面列表
     * @param menuId Integer
     * @return Model
     */
    @RequestMapping(value = API.URL_VIEWS, method = RequestMethod.POST)
    public Model views(@RequestParam Integer menuId) {
        try {
            return successModel(service.loadViews(menuId));
        } catch (Exception e){
            return failureModel(e);
        }
    }


    /**
     * 添加权限页
     * @param menuId Integer
     * @param viewJson String
     * @return Model
     */
    @RequestMapping(value = API.URL_ADD_VIEW, method = RequestMethod.POST)
    public Model addView(@RequestParam Integer menuId, @RequestParam String viewJson) {
        try {
            service.addView(menuId,viewJson);
            return successModel(Constant.ADD_SUCCESS,Constant.EMPTY);
        } catch (Exception e){
            return failureModel(e);
        }
    }


    /**
     * 更新权限页
     * @param viewJson String
     * @return Model
     */
    @RequestMapping(value = API.URL_UPDATE_VIEW, method = RequestMethod.POST)
    public Model updateView(@RequestParam String viewJson) {
        try {
            service.updateView(viewJson);
            return successModel(Constant.UPDATE_SUCCESS,Constant.EMPTY);
        } catch (Exception e){
            return failureModel(e);
        }
    }


    /**
     * 批量删除权限页面
     * @param deleteIdsJson String
     * @return Model
     */
    @RequestMapping(value = API.URL_DELETE_VIEWS, method = RequestMethod.POST)
    public Model deleteViews(@RequestParam String deleteIdsJson) {
        try {
            service.deleteViews(deleteIdsJson);
            return successModel(Constant.DELETE_SUCCESS,Constant.EMPTY);
        } catch (Exception e){
            return failureModel(e);
        }
    }

    /**
     * 权限项列表
     * @param viewId Integer
     * @return Model
     */
    @RequestMapping(value = API.URL_VIEW_ITEMS, method = RequestMethod.POST)
    public Model viewItems (@RequestParam Integer viewId) {
        try {
            return successModel(service.loadViewItems(viewId));
        } catch (Exception e){
            return failureModel(e);
        }
    }


    /**
     * 添加 页面权限项
     * @param viewId Integer
     * @param viewItemJson String
     * @return Model
     */
    @RequestMapping(value = API.URL_ADD_VIEW_ITEM, method = RequestMethod.POST)
    public Model addViewItem(@RequestParam Integer viewId,@RequestParam String viewItemJson){
        try {
            service.addViewItem(viewId,viewItemJson);
            return successModel(Constant.ADD_SUCCESS,Constant.EMPTY);
        } catch (Exception e){
            return failureModel(e);
        }
    }


    /**
     * 更新权限项
     * @param viewItemJson String
     * @return Model
     */
    @RequestMapping(value = API.URL_UPDATE_VIEW_ITEM, method = RequestMethod.POST)
    public Model updateViewItem(@RequestParam String viewItemJson) {
        try {
            service.updateViewItem(viewItemJson);
            return successModel(Constant.UPDATE_SUCCESS,Constant.EMPTY);
        } catch (Exception e){
            return failureModel(e);
        }
    }


    /**
     * 批量删除页面权限项
     * @param deleteIdsJson string
     * @return Model
     */
    @RequestMapping(value = API.URL_DELETE_VIEW_ITEMS, method = RequestMethod.POST)
    public Model deleteViewItems(@RequestParam String deleteIdsJson) {
        try {
            service.deleteViewItems(deleteIdsJson);
            return successModel(Constant.DELETE_SUCCESS,Constant.EMPTY);
        } catch (Exception e){
            return failureModel(e);
        }
    }

    @RequestMapping(value = API.URL_DELETE_VIEWS_OR_ITEMS, method = RequestMethod.POST)
    public Model deleteViewOrItems(@RequestParam String viewsJson) {
        try {
            List<View> views = jsonToListModel(viewsJson, View.class);
            service.deleteViewOrItems(views);
            return successModel(Constant.DELETE_SUCCESS,Constant.EMPTY);
        } catch (Exception e){
            return failureModel(e);
        }
    }


    @RequestMapping(value = API.URL_ROLE_VIEWS, method = RequestMethod.POST)
    public Model roleViews(@RequestParam Integer roleId, @RequestParam Integer menuId) {
        try {
            return successModel(service.loadRoleViews(roleId,menuId));
        } catch (Exception e){
            return failureModel(e);
        }
    }

    @RequestMapping(value = API.URL_UPDATE_ROLE_VIEWS, method = RequestMethod.POST)
    public Model updateRoleViews(@RequestParam Integer roleId, @RequestParam String viewsJson) {
        try {
            List<View> views = jsonToListModel(viewsJson, View.class);
            service.saveRoleViews(roleId,views);
            return successModel(Constant.UPDATE_SUCCESS,Constant.EMPTY);
        } catch (Exception e){
            return failureModel(e);
        }
    }


    @RequestMapping(value = API.URL_VIEW_AUTHORITY, method = RequestMethod.POST)
    public Model viewAuthority(@RequestParam String  path, String url) {
        try {
            Menu menu = menuService.loadMenuByByPath(path);
            List<ViewItem> items = service.loadAuthorityViewItems(menu.getId(),url);
            Model model = successModel();
            if (items != null) {
                items.forEach((ViewItem item) -> {
                    model.put(item.getProp()+"Show",item.isShowing());
                    model.put(item.getProp()+"Click",item.isClickable());
                });
            }
            return model;
        } catch (Exception e){
            return failureModel(e);
        }
    }

    @RequestMapping(value = API.URL_ALL_MENU_VIEWS, method = RequestMethod.POST)
    public Model allMenuViews(Integer type, Integer roleId) {
        List<Menu> menus = null;
        try {
            if (roleId == null) {
                menus = menuService.loadMenus();
            } else {
                menus = menuService.loadMenusOfRole(roleId);
            }
            return successModel(service.loadAllMenuViews(menus,roleId));
        } catch (Exception e){
            return failureModel(e);
        }
    }

    @RequestMapping(value = API.URL_UPDATE_ALL_MENU_VIEWS, method = RequestMethod.POST)
    public Model updateAllMenuViews(@RequestParam String menuViewsJson, Integer roleId) {
        try {
            List<Menu> menus = jsonToListModel(menuViewsJson,Menu.class);
            service.saveAllMenuViews(menus,roleId);
            return successModel(Constant.UPDATE_SUCCESS,Constant.EMPTY);
        } catch (Exception e){
            return failureModel(e);
        }
    }

    @RequestMapping(value = API.URL_DELETE_MENU_VIEWS, method = RequestMethod.POST)
    public Model deleteMenuViews(@RequestParam String menuViewsJson) {
        try {
            service.deleteMenuViews(menuViewsJson);
            return successModel(Constant.DELETE_SUCCESS,Constant.EMPTY);
        } catch (Exception e){
            return failureModel(e);
        }
    }

}

